package com.victor.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 * 
 * @author Mr.Zhang
 *
 */
public class DateUtils {

	// 字符串转date
	public static Date StrToDate(String datestr) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = formatter.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	// 获取当前时间字符串
	public static String getNowTime() {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return dateFormat.format(date);
	}

	// 获取当前时间2小时后时间
	public static String getTwoHourAfterTime() {
		long currentTime = System.currentTimeMillis() + 120 * 60 * 1000;
		Date date = new Date(currentTime);
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String twoHourAfterTime = df.format(date);
		return twoHourAfterTime;
	}

	// 字符串转date
	public static Date str2Date(String datestr) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = formatter.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static void main(String[] args) {
		System.out.println(getNowTime());
		System.out.println(getTwoHourAfterTime());
	}
}
